package com.awspaas.user.apps.processforsap.util;

import com.actionsoft.exception.BPMNError;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Base64;

public class HttpClientGwUtil {

    public static String doPost(String url, String json) throws Exception  {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        // 设置请求头部信息
        httpPost.addHeader("Content-Type", "application/json");



        // 设置请求体
        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity);

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }else{
                throw new BPMNError("0331","意外的接口请求错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BPMNError("0331",e.getMessage());
        } finally {
            httpClient.close();
        }
    }

    public static String doPost1(String url, String json,String token) throws Exception  {

        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);

        // 设置请求头部信息
        httpPost.addHeader("Content-Type", "application/json");

        // 添加 Basic Authentication 头部
        String authHeader = "Bearer " + token;
        httpPost.addHeader("Authorization", authHeader);

        // 设置请求体
        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
        httpPost.setEntity(entity);

        try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }else{
                throw new BPMNError("0331","意外的接口请求错误！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BPMNError("0331",e.getMessage());
        } finally {
            httpClient.close();
        }
    }


    public static String doGet(String url) throws IOException {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);

        try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                return EntityUtils.toString(responseEntity);
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw e;  // 可以根据实际情况处理异常
        } finally {
            httpClient.close();
        }
        return null;
    }

}
